﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace PstBakup2013Server
{
    internal class ClientMessage
    {
        private List<PstFile> _pstFiles = new List<PstFile>();

        #region {Properties}

        /// <summary>
        /// Gets or Sets the name of the Client.
        /// </summary>
        internal string ComputerName { get; set; }

        /// <summary>
        /// Gets or Sets the name of the user
        /// </summary>
        internal string UserName { get; set; }

        /// <summary>
        /// Gets or Sets the unique ID of the Client
        /// </summary>
        internal string ClientID { get; set; }

        /// <summary>
        /// Gets or Sets the version of the Client
        /// </summary>
        internal Version ClientVersion { get; set; }

        /// <summary>
        /// Gets or Sets the date when the message was sent
        /// </summary>
        internal DateTime SentDate { get; set; }

        /// <summary>
        /// Gets the list of all PstFiles
        /// </summary>
        internal List<PstFile> PstFiles { get { return this._pstFiles; } }

        #endregion {Properties}

        /// <summary>
        /// Constructor for the class
        /// </summary>
        /// <param name="xmlMessage">A XML formatted message sent by the Client.</param>
        internal ClientMessage(string xmlMessage)
        {
            this.ComputerName = String.Empty;
            this.UserName = String.Empty;
            this.ClientID = String.Empty;
            this.ClientVersion = new Version();
            this.SentDate = DateTime.Now;

            this.InitializeClientInformations(xmlMessage);
            this.InitializePstFiles(xmlMessage);
        }

        #region {Methods}

        /// <summary>
        /// Initialize Client related properties with the Xml Message sent by the Client
        /// </summary>
        /// <param name="message">Xml Message sent by the Client</param>
        private void InitializeClientInformations(string message)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(message);
                XmlNode root = doc.DocumentElement;
                XmlNode clientNode = root.SelectSingleNode("Client");
                XmlNode computerNameNode = root.SelectSingleNode("ComputerName");
                XmlNode userNameNode = root.SelectSingleNode("UserName");

                this.ComputerName = computerNameNode.InnerText;
                this.UserName = userNameNode.InnerText;
                this.ClientID = clientNode.Attributes["id"].Value;
                this.ClientVersion = new Version(clientNode.Attributes["ClientVersion"].Value);
                this.SentDate = Convert.ToDateTime(clientNode.Attributes["Date"].Value);
            }
            catch (Exception) { }
        }

        /// <summary>
        /// Initialize PstFiles list with the Xml Message sent by the Client
        /// </summary>
        /// <param name="message"></param>
        private void InitializePstFiles(string message)
        {
            this.PstFiles.Clear();

            XmlDocument doc = new XmlDocument();
            try
            {
                doc.LoadXml(message);
                XmlNode root = doc.DocumentElement;

                XmlNodeList fileNodes = root.SelectNodes("File");

                foreach (XmlNode node in fileNodes)
                {
                    try
                    {
                        this.PstFiles.Add(new PstFile(node));
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception) { }
        }

        #endregion {Methods}
    }
}
